#include "spi.h"

void SPI_FullDuplex_Init()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, ENABLE);
    SPI_InitTypeDef SPI_InitStructure = {0};
    RCC_APB1PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init (GPIOB, &GPIO_InitStructure);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init (SPI2, &SPI_InitStructure);
    SPI_Cmd (SPI2, ENABLE);
}

/* Hardware SPI */
uint8_t SPI_Transfer (uint8_t tx)
{
    uint8_t rx;

    while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_TXE) == RESET)
        ;
    SPI_I2S_SendData (SPI2, tx);

    while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_BSY) == SET)
        ;

    while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_RXNE) == RESET)
        ;
    rx = SPI_I2S_ReceiveData (SPI2);

    return rx;
}

void SPI_Transfer_Sync (uint8_t *dataout, uint8_t *datain, uint8_t len)
{
    for (int i = 0; i < len; i++)
    {
        while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_TXE) == RESET)
            ;
        SPI_I2S_SendData (SPI2, dataout[i]);
        while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_BSY) == SET)
            ;

        while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_RXNE) == RESET)
            ;
        datain[i] = SPI_I2S_ReceiveData (SPI2);
    }
}

void SPI_Transmit_Sync (uint8_t *dataout, uint8_t len)
{
    for (int i = 0; i < len; i++)
    {
        while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_TXE) == RESET)
            ;
        SPI_I2S_SendData (SPI2, dataout[i]);
        while (SPI_I2S_GetFlagStatus (SPI2, SPI_I2S_FLAG_BSY) == SET)
            ;
    }
}

extern void SPI_CSN_Pin_Init_For_Sensor2()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init (GPIOA, &GPIO_InitStructure);
    GPIO_SetBits (GPIOA, GPIO_Pin_8);
}

extern void SPI_CSN_Pin_Init_For_Sensor1()
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init (GPIOA, &GPIO_InitStructure);
    GPIO_SetBits (GPIOA, GPIO_Pin_15);
}